import { Module } from "vuex";
import { State } from "..";
import { Todo } from "../../../types";
import {getList} from "../../api/todos";

const initialState = {
    items: [] as Todo[]
}

export type TodoState = typeof initialState;

export default {
    namespaced:true,
    state:initialState,
    mutations:{
        initTodo(state,payload:Todo[]){
            console.log(payload);
            state.items = payload
        },
        addTodo(state,payload:Todo){
            state.items.push(payload)
        }
    },
    actions:{
        initTodo({commit}){
            getList().then((res:any)=>{
                if(res.code==0){
                    commit('initTodo',res.result)
               }
            })
        }
    }
} as Module<TodoState,State>;